package o2o.task.user;


import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import com.mongodb.BasicDBObject;
import o2o.config.dao.MongoDaoImpl;
import o2o.dao.PO.sys.LoginLog;

import java.util.ArrayList;
import java.util.List;

// 用户登录日志记录
public class UserLoginLogUtils {

    private static List<LoginLog> loginLogList = new ArrayList<>();


    public static void addLog(String username) {
        if (ValidateUtil.isNull(username)) {
            return;
        }
        LoginLog loginLog = new LoginLog();
        loginLog.setUsername(username);
        Long time = DateUtils.getCurrTime();
        loginLog.setLoginTime(time);
        loginLog.setCreateUser(username);
        loginLogList.add(loginLog);
    }


    public static void exeLogReal() {
        if (!ValidateUtil.isAbsList(loginLogList)) {
            return;
        }
        List<LoginLog> list = new ArrayList<>(loginLogList);
        loginLogList.clear();

        if (ValidateUtil.isAbsList(list)) {
            for (int i = 0; i < list.size(); i++) {
                LoginLog loginLog = list.get(i);
                if (null == loginLog) {
                    continue;
                }

                String username = loginLog.getUsername();
                if (ValidateUtil.isNull(username)) {
                    continue;
                }

                Long LogTime = loginLog.getLoginTime();
                if (ValidateUtil.isNotAbsLong(LogTime)) {
                    LogTime = DateUtils.getCurrTime();
                }

                // 更新mongoDB数据库
                BasicDBObject query = new BasicDBObject();
                query.append("username", username);

                BasicDBObject update = new BasicDBObject();
                update.append("username", username);
                update.append("loginDate", LogTime);
                update.append("loginDateStr", DateUtils.sdfDateTime.get().format(LogTime));
//                MongoDaoImpl.getInstance().update("records_user", "user_login_curr", query, update, true);
//                MongoDaoImpl.getInstance().insert("records_user", "user_login", update);
            }
        }


    }


}
